<?php

class WSB_Log
{

	/**
	 * @var WSB_Log
	 */
	private static $_instance;

	/**
	 * @var unknown_type
	 */
	private $_logger;

	private function __construct()
	{
		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$columnMapping = array(
			'priority' => 'priority',
			'message' => 'message',
			'beuser_id' => 'beuser_id',
			'ip' => 'ip',
			'module' => 'module',
			'controller' => 'controller',
			'action' => 'action',
			'server' => 'server',
			'exception' => 'exception',
			'created' => 'created',
			'deleted' => 'deleted'
		);
		$this->writer = new Zend_Log_Writer_Db($db, 'log', $columnMapping);
		$this->_logger = new Zend_Log($this->writer);

		// Beuser
		$beuserId = 0;
		$beuser = WSB_Auth_Backend::getInstance()->getIdentity();
		if($beuser instanceof Member_Model_DbTable_Beuser_Row) {
			$beuserId = $beuser->id;
		}
		$this->_logger->setEventItem('beuser_id', $beuserId);

		// IP
		$this->_logger->setEventItem('ip', $_SERVER['REMOTE_ADDR']);

		// Request
		$request = Zend_Controller_Front::getInstance()->getRequest();
		if($request instanceof Zend_Controller_Request_Http) {
			$this->_logger->setEventItem('module', $request->getModuleName());
			$this->_logger->setEventItem('controller', $request->getControllerName());
			$this->_logger->setEventItem('action', $request->getActionName());
		} else {
			$this->_logger->setEventItem('module', '');
			$this->_logger->setEventItem('controller', '');
			$this->_logger->setEventItem('action', '');
		}
		$this->_logger->setEventItem('created', time());
		$this->_logger->setEventItem('deleted', 0);

	}

	/**
	 * @return WSB_Log
	 */
	private static function _instance()
	{
		if(is_null(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	/**
	 * @param string $message
	 * @param integer $priority
	 * @param array $exceptions
	 */
	public static function log($message, $priority, $exceptions=null)
	{
		$instance = self::_instance();
		$exceptionStr = NULL;
		if(is_array($exceptions)) {
			foreach($exceptions AS $exception) {
				$exceptionStr .= $exception->__toString();
				$exceptionStr .= "\n\n";
			}
		}
		$instance->_logger->setEventItem('exception', $exceptionStr);

		// Server Variablen loggen, falls eine Exception geworfen wurde
		$serverStr = null;
		if(is_array($exceptions)) {
			$serverStr = '';
			foreach($_SERVER AS $key=>$value) {
				$serverStr .= sprintf("%s = %s\n", $key, $value);
			}
		}
		$instance->_logger->setEventItem('server', $serverStr);

		$instance->_logger->log($message, $priority);
	}

	/**
	 * @param string $message
	 * @param unknown_type $exceptions
	 */
	public static function emerg($message, $exceptions=null)
	{
		// 0
		self::log($message, Zend_Log::EMERG, $exceptions);
	}

	/**
	 * @param string $message
	 */
	public static function alert($message)
	{
		// 1
		self::log($message, Zend_Log::ALERT);
	}

	/**
	 * @param string $message
	 */
	public static function crit($message)
	{
		// 2
		self::log($message, Zend_Log::CRIT);
	}

	/**
	 * @param string $message
	 */
	public static function err($message)
	{
		// 3
		self::log($message, Zend_Log::ERR);
	}

	/**
	 * @param string $message
	 */
	public static function warn($message)
	{
		// 4
		self::log($message, Zend_Log::WARN);
	}

	/**
	 * @param string $message
	 */
	public static function notice($message)
	{
		// 5
		self::log($message, Zend_Log::NOTICE);
	}

	/**
	 * @param string $message
	 */
	public static function info($message)
	{
		// 6
		self::log($message, Zend_Log::INFO);
	}

	/**
	 * @param string $message
	 */
	public static function debug($message)
	{
		// 7
		self::log($message, Zend_Log::DEBUG);
	}

}
